import 'dart:typed_data';
import 'dart:ui';

import 'package:flutter/rendering.dart';
import 'package:flutter/services.dart';
import 'package:image_gallery_saver/image_gallery_saver.dart';
import 'package:jieqifang/global.dart';
import 'package:flutter/material.dart';
import 'package:jieqifang/iconfont.dart';
import 'package:jieqifang/toast.dart';

class DetailPage extends StatefulWidget {
  DetailPage({Key key, this.index}) : super(key: key);
  final index;

  @override
  _DetailPageState createState() => _DetailPageState();
}

class _DetailPageState extends State<DetailPage> {
  GlobalKey globalKey = GlobalKey();
  var _data;
  var _showBtn = true;

  Future<Uint8List> _capturePng() async {
    setState(() {
      _showBtn = false;
    });

    try {
      Future.delayed(Duration(milliseconds: 100), () async {
        RenderRepaintBoundary boundary =
            globalKey.currentContext.findRenderObject();
        var image = await boundary.toImage(pixelRatio: 3.0);
        ByteData byteData = await image.toByteData(format: ImageByteFormat.png);
        Uint8List pngBytes = byteData.buffer.asUint8List();
        final result = await ImageGallerySaver.saveImage(pngBytes,
            quality: 100, name: "节气坊" + ' ' + _data['name']);
        Toast.show(context,
            msg: '保存${result['isSuccess'] ? "成功" : "失败"}', duration: 1);
        setState(() {
          _showBtn = true;
        });
      });
    } catch (e) {
      print(e);
      setState(() {
        _showBtn = true;
      });
    }
    return null;
  }

  @override
  void initState() {
    setState(() {
      _data = Global.jieqiList[widget.index];
    });
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(
          _data['name'],
          style: TextStyle(
            color: Global.fontColor,
            fontSize: 19,
            fontFamily: 'QiuHongKai',
            fontWeight: FontWeight.bold,
          ),
        ),
        brightness: Brightness.light,
        iconTheme: IconThemeData(color: Global.fontColor),
        backgroundColor: Global.detailColor,
        elevation: 0,
        actions: <Widget>[
          IconButton(
            icon: Icon(
              Icons.file_download,
              color: Global.fontColor,
            ),
            tooltip: '保存图片',
            onPressed: () {
              _capturePng();
            },
          )
        ],
      ),
      body: Container(
        width: MediaQuery.of(context).size.width,
        height: MediaQuery.of(context).size.height,
        decoration: BoxDecoration(
          color: Global.detailColor,
        ),
        child: SingleChildScrollView(
          child: RepaintBoundary(
            key: globalKey,
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                Container(
                  width: MediaQuery.of(context).size.width,
                  padding: EdgeInsets.only(top: Global.paddingTop + 10),
                  color: Global.detailColor,
                  child: Column(
                    mainAxisAlignment: MainAxisAlignment.start,
                    crossAxisAlignment: CrossAxisAlignment.center,
                    children: [
                      Container(
                        width: MediaQuery.of(context).size.width - 150,
                        child: Image.asset('assets/images/${widget.index}.jpg'),
                      ),
                      Container(
                        child: Column(
                          children: [
                            Container(
                              // color: Global.backgroundColor,
                              padding: EdgeInsets.all(3),
                              child: Text(
                                _data['name'].split('').toList()[0],
                                style: TextStyle(
                                  color: Global.fontColor,
                                  fontSize: 32,
                                  fontFamily: 'QiuHongKai',
                                ),
                              ),
                            ),
                            Container(
                              color: _data['color'],
                              padding: EdgeInsets.all(3),
                              child: Text(
                                _data['name'].split('').toList()[1],
                                style: TextStyle(
                                  color: Global.backgroundColor,
                                  fontSize: 32,
                                  fontFamily: 'QiuHongKai',
                                ),
                              ),
                            ),
                            SizedBox(height: 20),
                            Row(
                              mainAxisAlignment: MainAxisAlignment.center,
                              crossAxisAlignment: CrossAxisAlignment.center,
                              children: [
                                Container(
                                  width: 25,
                                  child: Text(
                                    _data['shi'][0],
                                    style: TextStyle(
                                      color: Global.fontColor,
                                      fontSize: 22,
                                      fontFamily: 'QiuHongKai',
                                    ),
                                  ),
                                ),
                                SizedBox(width: 20),
                                Container(
                                  width: 25,
                                  child: Text(
                                    _data['shi'][1],
                                    style: TextStyle(
                                      color: Global.fontColor,
                                      fontSize: 22,
                                      fontFamily: 'QiuHongKai',
                                    ),
                                  ),
                                ),
                                SizedBox(width: 20),
                                Container(
                                  width: 25,
                                  child: Text(
                                    _data['shi'][2],
                                    style: TextStyle(
                                      color: Global.fontColor,
                                      fontSize: 22,
                                      fontFamily: 'QiuHongKai',
                                    ),
                                  ),
                                ),
                                SizedBox(width: 20),
                                Container(
                                  width: 25,
                                  child: Text(
                                    _data['shi'][3],
                                    style: TextStyle(
                                      color: Global.fontColor,
                                      fontSize: 22,
                                      fontFamily: 'QiuHongKai',
                                    ),
                                  ),
                                ),
                                SizedBox(width: 20),
                                Container(
                                  width: 25,
                                  child: Text(
                                    _data['shi'][4],
                                    style: TextStyle(
                                      color: Global.fontColor,
                                      fontSize: 25,
                                      fontFamily: 'QiuHongKai',
                                    ),
                                  ),
                                ),
                              ],
                            )
                          ],
                        ),
                      ),
                      SizedBox(height: 40),
                    ],
                  ),
                )
              ],
            ),
          ),
        ),
      ),
    );
  }
}
